package com.jsh.erp.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.json.JSONUtil;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.domain.model.TokenPayLoad;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class TokenUtil {

    private static byte[] key = new byte[]{1, 100, -35, -93, -21, 12, 121, -68, -48, -55, 25, -96, 58, -98, -15, -86};
    private static AES aes;
    public static final String ACCESS_TOKEN = "X-Access-Token";

    static {
        //        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
        aes = SecureUtil.aes(key);
//        String encryptHex = aes.encryptHex("content");
//        String decryptStr = aes.decryptStr("5b1ecdfc326a0ad084da3e9ccc1b65E2");
    }


    private TokenUtil() {
    }

    //只有登录接口能生成token
    public static String createToken(TokenPayLoad tokenPayLoad) {
        if (tokenPayLoad == null) {
            return "";
        }
        tokenPayLoad.setExpire(System.currentTimeMillis() + BusinessConstants.MAX_SESSION_IN_SECONDS * 1000L);
        tokenPayLoad.setVersion(1);
        return aes.encryptHex(JSONUtil.toJsonStr(tokenPayLoad));
    }

    public static String createToken(TokenPayLoad tokenPayLoad, String token) {
        if (tokenPayLoad == null) {
            return token;
        }
        TokenPayLoad payLoad = parseToken(token);
        if (tokenPayLoad.getExpire() != 0) {
            payLoad.setExpire(tokenPayLoad.getExpire());
        }
        return aes.encryptHex(JSONUtil.toJsonStr(payLoad));
    }

    public static TokenPayLoad parseToken(String token) {
        if (token == null) {
            return new TokenPayLoad();
        }
        TokenPayLoad tokenPayLoad = JSONUtil.toBean(aes.decryptStr(token), TokenPayLoad.class);
        if (tokenPayLoad.getExpire() < System.currentTimeMillis()) {
            //失效
            return new TokenPayLoad();
        }
        return tokenPayLoad;
    }

    public static TokenPayLoad parseToken(HttpServletRequest request) {
        if (request == null) {
            return new TokenPayLoad();
        }
        String token = request.getHeader(ACCESS_TOKEN);
        if (token == null) {
            return new TokenPayLoad();
        }
        TokenPayLoad tokenPayLoad = JSONUtil.toBean(aes.decryptStr(token), TokenPayLoad.class);
        if (tokenPayLoad.getExpire() < System.currentTimeMillis()) {
            //失效
            return new TokenPayLoad();
        }
        return tokenPayLoad;
    }

    public static TokenPayLoad parseTokenFromRequest() {
        if (RequestContextHolder.getRequestAttributes() == null) {
            return new TokenPayLoad();
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String token = request.getHeader(ACCESS_TOKEN);
        if (token == null) {
            return new TokenPayLoad();
        }
        TokenPayLoad tokenPayLoad = JSONUtil.toBean(aes.decryptStr(token), TokenPayLoad.class);
        if (tokenPayLoad.getExpire() < System.currentTimeMillis()) {
            //失效
            return new TokenPayLoad();
        }
        return tokenPayLoad;
    }


    public static void main(String[] args) {
        aes = SecureUtil.aes(key);
        String token = createToken(TokenPayLoad.builder().userId(123).build());
        System.out.println(token);
        System.out.println(parseToken(token));
        System.out.println(aes.decryptStr(token));
    }
}
